package logic

import (
	"context"
	"hotel/hotelrpc/hotelrpc"
	"hotel/pkg"
	"strconv"

	"hotel/api/internal/svc"
	"hotel/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type LonginLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewLonginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LonginLogic {
	return &LonginLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *LonginLogic) Longin(req *types.LonginRequest) (resp *types.Response, err error) {
	// todo: add your logic here and delete this line
	longin, err := l.svcCtx.HotelRpc.Longin(l.ctx, &hotelrpc.LonginRequest{
		Name: req.Name,
		Tel:  req.Phone,
		Send: req.Send,
	})
	if err != nil {
		return &types.Response{
			Message: err.Error(),
			Code:    "500",
			Data:    nil,
		}, nil
	}
	handler, err := pkg.TokenHandler(strconv.FormatInt(longin.Id, 10))
	if err != nil {
		return &types.Response{
			Message: err.Error(),
			Code:    "500",
			Data:    nil,
		}, nil
	}

	return &types.Response{
		Message: "登陆成功",
		Code:    "200",
		Data:    handler,
	}, nil
}
